package com.server.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.service.ICommonDataService;
import com.server.service.IOrgManagementService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("OrgManagementService")
public class OrgManagementService implements IOrgManagementService {

	private static final String TABLE_NAME = "reward.org";

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;

	/**
	 * 公司添加
	 * @param operator
	 * @param inputMap
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Boolean add(String operator, Map<String, Object> inputMap) {
		Object orgObj = inputMap.get("OrgNo");
		Object pNoObj = inputMap.get("ParentName");
		if (orgObj == null) {
			return false;
		}
		if (pNoObj != null) {
			String[] split = pNoObj.toString().split("serverSplit");
			inputMap.put("ParentNo", split[0]);
			inputMap.put("Grade", Integer.parseInt(split[1])+1);
		}
		String orgNo = orgObj.toString();
		// 查询是否可添加
		Boolean verify = this.verify(orgNo);
		return verify && commonDataService.add(operator, TABLE_NAME, inputMap);
	}

	/**
	 * 公司数据修改
	 * @param operator
	 * @param inputMap
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Boolean update(String operator, Map<String, Object> inputMap) {
		Object orgObj = inputMap.get("OrgNo");
		if (orgObj == null) {
			return false;
		}
		String orgNo = orgObj.toString();
		return commonDataService.update(operator, TABLE_NAME, "OrgNo", orgNo,
				inputMap);
	}

	/**
	 * 查询能否添加
	 * @param orgNo
	 * @return
	 * @author ServerZhang
	 * @date 2017年3月3日
	 */
	@Override
	public Boolean verify(String orgNo) {
		if (StringUtils.isEmpty(orgNo)) {
			return false;
		}
		String sql = "select count(OrgNo) from %s where OrgNo='%s'";
		return jdbcTemplate.queryForObject(
				String.format(sql, TABLE_NAME, orgNo), Integer.class) < 1;
	}

	@Override
	public Map<String, Object> delete(String orgNos) {
		Boolean isSuccess = false;
		Integer errorCount = 0;
		Map<String, Object> returnMap = new HashMap<String, Object>();
		if (!StringUtils.isEmpty(orgNos)) {
			int tCount = orgNos.split(",").length;
			StringBuffer sqlBuffer = new StringBuffer();
			sqlBuffer.append("delete from ").append(TABLE_NAME)
					.append(" where OrgNo in('")
					.append(orgNos.replaceAll(",", "','")).append("')");
			int uCount = jdbcTemplate.update(sqlBuffer.toString());
			if ((errorCount = tCount - uCount) == 0) {
				isSuccess = true;
			}
		}
		returnMap.put("isSuccess", isSuccess);
		returnMap.put("errorCount", errorCount);
		return returnMap;
	}

	@Override
	public DataVector<Map<String, Object>> getList(RequestInput requestInput) {
		String whereStr = null;
		String sortStr = "CreateTime asc";
		return commonDataService.getListData(TABLE_NAME, whereStr, sortStr,
				requestInput);
	}
	@Override
	public List<Map<String, Object>> getList(){
		String sql = "select * from %s where Valid=1 order by EmployState desc";
		return jdbcTemplate.queryForList(String.format(sql, TABLE_NAME));
	}
	@Override
	public List<Map<String, Object>> getList(Integer userGrade,String orgNo){
		if (userGrade.equals(9)) {//超级管理员
			return this.getList();
		}else if (userGrade.equals(1)||userGrade.equals(2)) {//普工和招聘人员
			return new ArrayList<Map<String,Object>>();
		}
		String sql = "select * from %s where Valid=1 and OrgNo='%s'";
		return jdbcTemplate.queryForList(String.format(sql, TABLE_NAME, orgNo));
	}
	@Override
	public List<Map<String, Object>> getAllOrg(){
		String sql = "select OrgNo, Grade, OrgName from %s where Valid=1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME));
		Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object noObj = next.get("OrgNo");
			Object gradeObj = next.get("Grade");
			if (noObj != null && noObj != null) {
				next.put("OrgNo", noObj.toString()+"serverSplit"+gradeObj.toString());
			}
		}
		return list;
	}

	@Override
	public Map<String, Object> getForm(String orgNo) {
		String sql = "select base.*,p.OrgName as ParentName from %s base left outer join %s p on base.ParentNo=p.OrgNo where base.OrgNo='%s' and base.Valid=1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME,TABLE_NAME,orgNo));
		Iterator<Map<String, Object>> iterator = list.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object noObj = next.get("ParentNo");
			Object gradeObj = next.get("Grade");
			if (noObj != null && noObj != null) {
				next.put("ParentName", noObj.toString()+"serverSplit"+(Integer.parseInt(gradeObj.toString())-1));
			}
		}
		return list.get(0);
	}
	@Override
	public String getOrg4User(String cardID){
		String sql = "select OrgNo from %s where CardID='%s' and State=1 and OrgNo is not null";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, "reward.employ",cardID));
		if (list.size() > 0) {
			return list.get(0).get("OrgNo").toString();
		}else {
			return "";
		}
	}
}
